﻿using PomodoroTimer.Properties;
using System;
using System.Windows.Forms;

namespace PomodoroTimer
{
    public partial class FormMain : Form
    {
        private static readonly TimeSpan WORKING_TIME = new TimeSpan(00, 25, 00);
        private static readonly TimeSpan REST_TIME = new TimeSpan(00, 5, 00);
        private static readonly TimeSpan LONG_REST_TIME = new TimeSpan(00, 30, 00);
        private static readonly int NB_POMODORO_TO_COMPLETE_FOR_LONG_REST = 4;
        
        // state : 0 = waiting, 1 = paused, 2 = running
        private int state = 0;
        private int nbCompletedPomodoro = 0;
        private bool isResting = false;
        private DateTime targetDate;

        public FormMain()
        {
            InitializeComponent();

            Text = Resources.Pomodoro_Timer;
            labelTime.Text = GetTimeLeft(WORKING_TIME);
            labelCurrentState.Text = Resources.Waiting_to_start;
            notifyIconWindows.Text = Resources.Pomodoro_Timer;
        }

        private void btStart_Click(object sender, EventArgs e)
        {
            if (state != 2)
            {
                if (state == 0)
                {
                    isResting = false;
                    targetDate = DateTime.UtcNow.Add(WORKING_TIME);
                }
                timerRunning.Start();

                labelCurrentState.Text = isResting ? Resources.Resting : Resources.Working;

                state = 2;
            }
        }

        private void btPause_Click(object sender, EventArgs e)
        {
            if (state != 1)
            {
                timerRunning.Stop();
                state = 1;
                labelCurrentState.Text = Resources.Paused;
            }
        }

        private void btStop_Click(object sender, EventArgs e)
        {
            if (state != 0)
            {
                timerRunning.Stop();
                state = 0;
                nbCompletedPomodoro = 0;
                labelCurrentState.Text = Resources.Waiting_to_start;
                labelTime.Text = GetTimeLeft(WORKING_TIME);
            }
        }

        private void timerRunning_Tick(object sender, EventArgs e)
        {
            if (targetDate <= DateTime.UtcNow)
            {
                if (isResting)
                {
                    timerRunning.Stop();
                    nbCompletedPomodoro++;
                    MessageBox.Show(Resources.Let_s_work_from_now, Resources.Pomodoro);

                    targetDate = DateTime.UtcNow.Add(WORKING_TIME);
                }
                else
                {
                    timerRunning.Stop();
                    MessageBox.Show(Resources.You_can_rest, Resources.Pomodoro);

                    if (nbCompletedPomodoro + 1 == NB_POMODORO_TO_COMPLETE_FOR_LONG_REST)
                    {
                        targetDate = DateTime.UtcNow.Add(LONG_REST_TIME);
                        nbCompletedPomodoro = -1;
                    }
                    else
                    {
                        targetDate = DateTime.UtcNow.Add(REST_TIME);
                    }
                }
                timerRunning.Start();
                isResting = !isResting;
                labelCurrentState.Text = isResting ? Resources.Resting : Resources.Working;
            }

            var timeLeft = targetDate - DateTime.UtcNow;
            labelTime.Text = GetTimeLeft(timeLeft);
            notifyIconWindows.Text = Resources.Pomodoro + " - " + labelCurrentState.Text + " - " + labelTime.Text;
        }

        private static string GetTimeLeft(TimeSpan time)
        {
            return (time.Hours >= 10 ? time.Hours.ToString() : "0" + time.Hours)
                + ":" + (time.Minutes >= 10 ? time.Minutes.ToString() : "0" + time.Minutes) + ":"
                + (time.Seconds >= 10 ? time.Seconds.ToString() : "0" + time.Seconds);
        }

        private void notifyIconWindows_DoubleClick(object sender, EventArgs e)
        {
            Show();
        }

        private void btHide_Click(object sender, EventArgs e)
        {
            Hide();
        }
    }
}